package com.example.tests;

import java.io.BufferedReader;
import static com.example.fw.RandomGenerator.generateString;

import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import com.thoughtworks.xstream.XStream;

public class GroupDataGenerator {

	public static void main(String[] args) throws IOException {
		if (args.length < 3) {
			System.out.println("Please specify perameters: <amount of test data> <file> <format>");
			return;
		}
		
		int amount = Integer.parseInt(args[0]);
		File file = new File(args[1]);
		String format = args[2];
		
		if (file.exists()){
			System.out.println("File exists, please remove it manually: "+file);
		}
			
		List<GroupData> groups = generateRandomGroups(amount);
		if (format.equals("csv")) {
			saveGroupsToCsvFile(groups,file);
		} else if (format.equals("xml")) {
			saveGroupsToXmlFile(groups,file);
		} else {
			System.out.println("Uknown format "+format);
			return;
		}
	}

	private static void saveGroupsToXmlFile(List<GroupData> groups, File file) throws IOException {
		XStream xs = new XStream();
		xs.alias("group", GroupData.class);
		String xml = xs.toXML(groups);
		FileWriter writer = new FileWriter(file);
		writer.write(xml);
		writer.close();
	}

	public static List<GroupData> loadGroupsFromXmlFile(File file) {
		XStream xs = new XStream();
		xs.alias("group", GroupData.class);
		return (List<GroupData>) xs.fromXML(file);
	}

	private static void saveGroupsToCsvFile(List<GroupData> groups, File file) throws IOException {
		FileWriter writer = new FileWriter(file);
		for (GroupData group : groups) {
			writer.write(group.getName()+","+group.getHeader()+","+group.getFooter()+",!\n");
		}
		writer.close();
	}

	public static List<GroupData> generateRandomGroups(int amount) {
		List<GroupData> list = new ArrayList<GroupData>();
		for (int i = 0; i < amount; i++){
			GroupData group = new GroupData()
				.withName(generateString())
				.withHeader(generateString())
				.withFooter(generateString());
			list.add(group);
		}
		
		return list;
	}
	
	public static List<GroupData> loadGroupsFromCsvFile(File file) throws IOException {
		List<GroupData> list = new ArrayList<GroupData>();
		FileReader reader = new FileReader(file);
		BufferedReader bufferedReder = new BufferedReader(reader);
		String line = bufferedReder.readLine();
		while (line != null){
			String[] part = line.split(",");
			GroupData group = new GroupData()
				.withName(part[0])
				.withHeader(part[1])
				.withFooter(part[2]);
			list.add(group);
			line = bufferedReder.readLine();
		}
		bufferedReder.close();
		return list;
	}

}
